<?php

namespace app\admin\controller\member;

use app\admin\model\UM as UM;
use app\admin\service\MemberService;
use ShidianUtil\Thinkphp\Response\ApiResponse;
use ShidianUtil\Utils\CheckUtil;
use think\APP;
use think\Request as Request;

/**
 * Member 控制器
 * Class Member
 * @package app\member\controller
 * @link https://www.kancloud.cn/manual/thinkphp6_0/1037509
 */
class Member
{
    private Request $request;

    private MemberService $service;

    public function __construct(Request $request, MemberService $memberService)
    {
        $this->request = $request;
        $this->service = $memberService;
    }

    /**
     * 用户管理-普通用户-列表
     * @param \app\admin\model\UM|null $user
     * @return ApiResponse
     * @url /member/member/index
     */
    public function index(?UM $user): ApiResponse
    {
        $request = $this->request;

        $channel = $request->param('channel');
        $chain = $request->param('chain');
        $provinceId = $request->param('provinceId');
        $cityId = $request->param('cityId');
        $startTime = $request->param('startTime');
        $endTime = $request->param('endTime');
        $kw = $request->param('kw');
        $inviteId = $request->param('inviteId');

        $page = $request->param('page', 1);
        $page_size = $request->param('page_size', 20);

        return ApiResponse::success($this->service->index($user, $channel, $chain, $provinceId, $cityId, $startTime, $endTime, $kw, $inviteId,  $page, $page_size));
    }

    /**
     * 详情
     * @param \app\admin\model\UM|null $user
     * @return ApiResponse
     * @url /member/member/read
     */
    public function read(?UM $user): ApiResponse
    {
        $request = $this->request;

        $id = $request->param('id');

        if (!CheckUtil::isId($id)) {
            return ApiResponse::fail('缺少id参数');
        }

        return ApiResponse::success($this->service->read($user, $id));
    }
}
